<?php

namespace app\models;

use think\Db;
use think\Model;

class User extends Model
{
    protected $pk = 'id';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';
    protected $defaultSoftDelete = 0;

    /**
     * 用户积分|冻结积分变更记录
     * @param $user_id   // 用户
     * @param $comeuser_id   // 来源ID
     * @param $befores   // 变化前资产
     * @param $money   // 变化金额
     * @param $cointype   // 币种类型
     * @param $handfee   // 手续费
     * @param $type   // 变化类型
     * @param $info   //备注
     * @param $source   // 资产来源ID
     * @param $algebra   // 收益来源代数
     * @return void
     */
    public static function IntergralEdit($user_id = 0, $befores = 0, $money = 0, $type = 0, $source = 0, $info = '')
    {
        if ($money != 0) {
            $res = (new User())->where(['id' => $user_id, 'integral' => $befores])->setInc('integral', $money);
            $res2 = (new IntergralLog())->insert([
                'logid' => time(),
                'user_id' => $user_id,
                'befores' => $befores,
                'money' => $money,
                'after' => ($befores + $money),
                'type' => $type,
                'info' => $info,
                'dates' => date('Y-m-d'),
                'sourceid' => $source
            ]);
            if (!$res || !$res2) {
                throw new \Exception("积分资产修改失败，请重试", 100);
            }
        }
    }
}
